package com.example.config;

import com.example.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Description:统一异常处理
 * User: 阿宾
 * Date: 2025-05-12
 * Time: 20:02
 */
@Slf4j
@ControllerAdvice
@ResponseBody //返回数据
public class ExceptionAdvice {

    /**
     * 用来对没有处理的异常进行统一处理
     * @param e
     * @return
     */
    @ExceptionHandler
    public Result handlerException(Exception e){
        log.error("发生异常：e:",e);
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler
    public Result handlerException(NullPointerException e){
        log.error("发生异常：e:",e);
        return Result.fail("发生NullPointerException异常"+e.getMessage());
    }
    @ExceptionHandler
    public Result handlerException(ArithmeticException e){
        log.error("发生异常：e:",e);
        return Result.fail("发生ArithmeticException异常"+e.getMessage());
    }
}
